package com.wyz.saas.app.mvvm.list

import android.os.Bundle
import android.view.View
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.atom.module.mvvm.base.AbstractBaseActivity
import com.atom.module.mvvm.extren.isNo
import com.atom.module.mvvm.extren.isYes
import com.atom.module.mvvm.wight.recycler.multi.MultiRecyclerAdapter
import com.atom.module.mvvm.wight.recycler.multi.MultiRecyclerAdapterListener
import com.wyz.saas.app.net.NewsCardData
import com.wyz.saas.app.mvvm.ViewModelFactory
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.wyz.saas.app.R
import com.wyz.saas.app.databinding.ActivityListBinding
import com.wyz.saas.app.databinding.RecyclerItemNewsBigImageBinding
import com.wyz.saas.app.databinding.RecyclerItemNewsBinding
import com.wyz.saas.common.base.AbstractApiActivity
import com.wyz.saas.common.utils.Logs
import com.wyz.saas.common.view.EmptyView


class ListActivity : AbstractApiActivity<ActivityListBinding, ListViewModel>() {

    private var mEmptyView: EmptyView? = null

    override fun initContentView(savedInstanceState: Bundle?): Int {
        return R.layout.activity_list
    }

    override fun initViewModel(): ListViewModel {
        return ViewModelProvider(
            this.viewModelStore,
            ViewModelFactory.sInstance
        ).get(ListViewModel::class.java)
    }

    override fun initViewBinding(binding: ActivityListBinding?, viewModel: ListViewModel?) {
        super.initViewBinding(binding, viewModel)
        binding?.also {
            // 布局刷新
            it.refreshLayout.setRefreshHeader(ClassicsHeader(this))
            it.refreshLayout.setRefreshFooter(ClassicsFooter(this))

            it.refreshLayout.setOnRefreshListener { refreshLayout ->
                refreshLayout.finishRefresh(1000 /*,false*/) //传入false表示刷新失败

            }
            it.refreshLayout.setOnLoadMoreListener { refreshlayout ->
                refreshlayout.finishLoadMore(2000 /*,false*/) //传入false表示加载失败
                val currSize = it.listView.adapter?.itemCount ?: 0
                val currPage = currSize.div(30) + 1
                viewModel?.loadMoreNewsData(currPage)
            }
            it.listView.layoutManager = LinearLayoutManager(this)
            it.listView.adapter =
                object : MultiRecyclerAdapter<NewsCardData>() {
                    override fun getItemViewType(position: Int, data: NewsCardData): Int {
                        return (position % 2 == 0).isYes {
                            R.layout.recycler_item_news_big_image
                        }.isNo {
                            R.layout.recycler_item_news
                        }
                    }

                    override fun onCreateMultiViewHolder(
                        binding: ViewDataBinding,
                        viewType: Int
                    ): ViewHolder<NewsCardData> {
                        if (viewType == R.layout.recycler_item_news_big_image) {
                            return NewsBigViewHolder(binding as RecyclerItemNewsBigImageBinding)
                        }
                        return NewsViewHolder(binding as RecyclerItemNewsBinding)
                    }

                }.also {
                    it.listener =
                        object : MultiRecyclerAdapterListener<NewsCardData>() {
                            override fun onMultiItemClick(viewHolder: MultiRecyclerAdapter.ViewHolder<NewsCardData>) {
                                Logs.e("点击了", viewHolder.mData?.date)
                            }

                            override fun onMultiItemLongClick(viewHolder: MultiRecyclerAdapter.ViewHolder<NewsCardData>): Boolean {
                                Logs.e("长按了", viewHolder.mData?.date)
                                return super.onMultiItemLongClick(viewHolder)
                            }
                        }
                }
        }
    }

    class NewsViewHolder(viewBinding: RecyclerItemNewsBinding) :
        MultiRecyclerAdapter.ViewHolder<NewsCardData>(viewBinding) {

        override fun onBind(data: NewsCardData) {
            if (binding is RecyclerItemNewsBinding) {
                binding.newsCardBean = data
            }
        }
    }

    class NewsBigViewHolder(viewBinding: RecyclerItemNewsBigImageBinding) :
        MultiRecyclerAdapter.ViewHolder<NewsCardData>(viewBinding) {

        override fun onBind(data: NewsCardData) {
            if (binding is RecyclerItemNewsBigImageBinding) {
                binding.newsCardBean = data
            }
        }
    }

    override fun initData(view: View?) {
        super.initData(view)
        view?.also { root ->
            mEmptyView = root.findViewById(R.id.tag_empty_view)
            mEmptyView?.bind(root.findViewById(R.id.refreshLayout))
        }
        viewModel?.also {
            it.listdata.observe(this, { card ->
                val adapter = binding?.listView?.adapter
                if (adapter is MultiRecyclerAdapter<*>) {
                    (adapter as MultiRecyclerAdapter<NewsCardData>).addAll(
                        card.value.data!!
                    )
                }
            })
        }
    }

    override fun showLoading() {
        mEmptyView
            ?.triggerLoading()
            ?: let {
                super.showLoading()
            }
    }

    override fun hideLoading() {
        mEmptyView
            ?.triggerOk()
            ?: let {
                super.hideLoading()
            }
    }

    override fun showError(strError: String) {
        mEmptyView
            ?.triggerError(strError)
            ?: let {
                super.showError(strError)
            }
    }
}